package com.welove.widget;

import com.welove.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;

/**
 * @ClassName: ChoosePicPopupWindow
 * @Description: 仿微信弹出底部框Popupwindow
 * @author: dreamfutureba@gmail.com
 * @date: 2013年11月8日 上午10:36:51
 */
public class ChoosePicPopupWindow extends PopupWindow {
	private static final String TAG = ChoosePicPopupWindow.class
			.getSimpleName();

	private Button view_choose_from_camera;
	private Button view_choose_from_albums;
	private Button view_choose_cancel;

	private View mMenuView;

	public ChoosePicPopupWindow(Activity context, OnClickListener itemsOnClick) {
		super(context);
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mMenuView = inflater.inflate(R.layout.view_choose_pop, null);

		view_choose_from_camera = (Button) mMenuView
				.findViewById(R.id.view_choose_from_camera);
		view_choose_from_albums = (Button) mMenuView
				.findViewById(R.id.view_choose_from_albums);
		view_choose_cancel = (Button) mMenuView
				.findViewById(R.id.view_choose_cancel);
		// 取消按钮
		view_choose_cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 销毁弹出框
				dismiss();
			}
		});
		// 设置按钮监听
		view_choose_from_camera.setOnClickListener(itemsOnClick);
		view_choose_from_albums.setOnClickListener(itemsOnClick);
		// 设置SelectPicPopupWindow的View
		this.setContentView(mMenuView);
		// 设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(LayoutParams.FILL_PARENT);
		// 设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(LayoutParams.WRAP_CONTENT);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		// 设置SelectPicPopupWindow弹出窗体动画效果
		this.setAnimationStyle(R.style.AnimBottom);
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(0xb0000000);
		// 设置SelectPicPopupWindow弹出窗体的背景
		this.setBackgroundDrawable(dw);
		mMenuView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				int height = mMenuView.findViewById(R.id.view_choose_pop_layout).getTop();
				int y=(int) event.getY();
				if(event.getAction()==MotionEvent.ACTION_UP){
					if(y<height){
						dismiss();
					}
				}
				return true;
			}
		});
	}
}
